package nzb;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

public class NzbParser {

	private String 				filePath;
	private List<NzbFile> 		nzbFiles;
	private SAXParserFactory 	parserFactory;
	private SAXParser 			parser;
	private File 				nzbFileToParse;
	private NzbHandler			nzbHandler;
	
	public NzbParser(String filePath) {
		this.filePath 		= filePath;
		this.parserFactory	= SAXParserFactory.newInstance();
		this.nzbFileToParse 	= new File(this.filePath);
		this.nzbHandler		= new NzbHandler();
	}
	
	public void parse() throws ParserConfigurationException, SAXException, IOException
	{
		this.parser			= this.parserFactory.newSAXParser();
		this.parser.getXMLReader().setFeature("http://xml.org/sax/features/validation", false);
		this.parser.getXMLReader().setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
		this.parser.getXMLReader().setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
		this.parser.parse(this.filePath, this.nzbHandler);
		this.nzbFiles		= this.nzbHandler.getFiles();
	}
	
	public List<NzbFile> getFiles()
	{
		return this.nzbFiles;
	}
}
